<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible" @close="ss()">
    {{dataForm}}----
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
    <el-form-item label="规格名称" prop="spec">
      <el-input v-model="dataForm.spec.specName" placeholder="规格名称"></el-input>
    </el-form-item>
    </el-form>
    <el-button type="primary" plain @click="dataForm.option.push({})">新增规格</el-button>
    <el-table
      :data="dataForm.option"
      style="width: 100%">
      <el-table-column
        prop="date"
        label="规格选项"
        width="220"
        align="center">
        <template slot-scope="scope">
          <el-input v-model="scope.row.optionName"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="排序"
        width="220"
        align="center">
        <template slot-scope="scope">
          <el-input v-model="scope.row.orders"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        prop="address"
        label="操作"
        align="center">
        <template slot-scope="scope">
          <el-button type="danger" icon="el-icon-delete" circle @click="dataForm.option.splice(scope.index,1)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button @click="ss()">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          spec: {      //代表规格对象
            specName: '',
          },
          option:[]    //代表规格对象下的规格选项集合
        },
        dataRule: {
            spec: [
              { required: true, message: '名称不能为空', trigger: 'blur' }
            ]
          }

      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/manager/specification/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.spec = data.specification.specificationEntity;
                this.dataForm.option = data.specification.specificationOptionEntityList;
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        let data = this.$http.adornData({
          'id': this.dataForm.id || undefined,
          'specificationEntity': this.dataForm.spec,
          'specificationOptionEntityList': this.dataForm.option
        })
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/manager/specification/${!this.dataForm.id ? 'save' : 'update'}`),
              method: `${this.dataForm.id ? 'put' : 'post'}`,
              data
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      ss(){
        this.dataForm.spec = '';
        this.dataForm.option = [];
        this.visible= false
      }
    }
  }
</script>
